Question 1
Recall the airport locations and flights data from the lecture on maps:
suppressMessages(library(plotly))
# Airport locations:
air <- readRDS("HW8_airport_locations.rds")
# Flight paths
flights <- readRDS("HW8_flights.rds")
# Map projection:
geo <-
list(
projection = list(
type = 'orthographic',
rotation = list(lon = -100, lat = 40, roll = 0)
),
showland = TRUE,
landcolor = toRGB("gray95")
)
plot_geo(color = I("red")) %>%
add_segments(
data = flights,
x = ~start_lon,
xend = ~end_lon,
y = ~start_lat,
yend = ~end_lat,
alpha = 0.3,
size = I(1)
) %>%
add_markers(
data = air,
x = ~long,
y = ~lat,
size = ~cnt,
alpha = 0.5
) %>%
layout(geo = geo, showlegend = FALSE)
## Warning: `line.width` does not currently support multiple values.
Use plotly to create a graph representation of flight
data network.
If you try to convert the two data frames to a graph object, a problem will be revealed. What is it? Fix it by adding some values to one or both of the dataframes.
Hint: if you want to augment a certain variable in a dataframe, say the variable at the 6th column, while adding NA values to the other columns, you could:
dataframe[(nrow(dataframe) + 1):(nrow(dataframe) + length(additional_values)), 6] <- additional_values
Set the color of a node according to its centrality.
Comment on the layout you chose to employ and your selection of a centrality measure. What do you find it easier to see in the graph representation compared to the geographical representation?
Question 2
Create a nice representation of the UKFaculty graph using visNetwork.
For fun, add random names from babynames to each node and
have those reflected in the tooltip.
The network is given as a directed and weighted network. Please consider these attributes when visualizing the network. You can consider ignoring these attributes with the analytical functions you utilize.
Experiment with a dynamic layout (?visPhysics)
vs. igrpah layouts (?visIgraphLayout). Provide
the best of each according to your judgment, and explain why you prefer
those to other layouts.
Try some community detection algorithms from igraph with it. Provide
a visual comparison of the results of the best community detection
algorithm and the worst community detection algorithm with the actual
groups the providers of the data set identified (the Group
attribute of the vertices).
Note: you are not asked to quantify the accuracy of the community detection algorithms rigorously, only to choose the visual representations which seem the closest and the farthest from the true community structure.
To do this quantitatively, you may utilize an extended notion of confusion matrix, which will be demonstrated in class next week.
# Some ingredients:
suppressMessages(library(igraph))
library(igraphdata)
data("UKfaculty")
library(visNetwork)
vg <- toVisNetworkData(UKfaculty, idToLabel = FALSE)
## This graph was created by an old(er) igraph version.
## Call upgrade_graph() on it to use with the current igraph version
## For now we convert it on the fly...
head(vg$nodes)
## id Group
## 1 1 3
## 2 2 1
## 3 3 3
## 4 4 3
## 5 5 2
## 6 6 2
head(vg$edges)
## from to weight
## 1 57 52 4
## 2 76 42 14
## 3 12 69 4
## 4 43 34 4
## 5 28 47 10
## 6 58 51 2
bb <- babynames::babynames
set.seed(697)
# Assign this to the nodes:
sample(bb$name, vcount(UKfaculty))
## [1] "Lucerito" "Randall" "Elias" "Mckinzee" "Lindsay"
## [6] "Dawsyn" "Montserrat" "Jenika" "Marion" "Ashle"
## [11] "Riley" "Tysha" "Jaquincy" "Jandiel" "Gean"
## [16] "Janetta" "Bradley" "Remigio" "Tiwan" "Aydien"
## [21] "Gerise" "Kacie" "Judine" "Rajanae" "Tashiana"
## [26] "Pat" "Nyra" "Dinah" "Gricelda" "Amil"
## [31] "Modesto" "Doniesha" "Jaymes" "Marvyn" "Shahin"
## [36] "Theta" "Arlen" "Yamato" "Eben" "Ma"
## [41] "Kedrick" "Marcelo" "Kenn" "Cam" "Trysta"
## [46] "Raylene" "Hussam" "Jake" "Arabella" "Mechele"
## [51] "Marwa" "Durand" "Tiffanyamber" "Ruhi" "Deavan"
## [56] "Takai" "Dauna" "Mala" "Kearah" "Jory"
## [61] "Eunice" "Dollie" "Carissa" "Thornton" "Ellarie"
## [66] "Jarica" "Shifra" "Tradarrius" "Jansel" "Alline"
## [71] "Knightly" "Jettie" "Azeem" "Warren" "Erwin"
## [76] "Markai" "Jannessa" "Renessa" "Deeanne" "Carvel"
## [81] "Devona"
# igraph community detection:
# Which works for directed and/or weighted network?
# Community detection functions in igraph start with `cluster_`